<?php

namespace Welcomescreen\Objects;

class TemplateVariable {
	const TYPE_TEXT = 'text';
	const TYPE_IMAGE = 'image';
	
	static $availableTypes = array(
		self::TYPE_TEXT,
		self::TYPE_IMAGE,
	);
	
	/**
	 * @var string
	 */
	protected $name;
	
	/**
	 * @var string
	 */
	protected $field;
	
	/**
	 * @var string
	 */
	protected $type;
	
	/**
	 * @var bool
	 */
	protected $optional = false;
	
	public function __construct($name, $field, $type, $optional = false) {
		$this->setName($name);
		$this->setField($field);
		$this->setType($type);
		$this->setOptional($optional);
	}
	
	public function toArray() {
		return array(
			'name' => $this->getName(),
			'field' => $this->getField(),
			'type' => $this->getType(),
			'optional' => $this->isOptional(),
		);
	}
	
	/**
	 * @return string
	 */
	public function getName() {
		return $this->name;
	}
	
	/**
	 * @param string $name
	 */
	public function setName($name) {
		$this->name = $name;
	}
	
	/**
	 * @return string
	 */
	public function getField() {
		return $this->field;
	}
	
	/**
	 * @param string $field
	 */
	public function setField($field) {
		$this->field = $field;
	}
	
	/**
	 * @return string
	 */
	public function getType() {
		return $this->type;
	}
	
	/**
	 * @param string $type
	 */
	public function setType($type) {
		if(!in_array($type, self::$availableTypes)) {
			throw new \InvalidArgumentException('Invalid type supplied. Given was "' . $type . '" required is one of ' . implode(', ', self::$availableTypes));
		}
		$this->type = $type;
	}
	
	/**
	 * @return bool
	 */
	public function isOptional() {
		return $this->optional;
	}
	
	/**
	 * @param bool $optional
	 */
	public function setOptional($optional = true) {
		$this->optional = $optional;
	}
}

?>